<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';

/**
 * 会員登録のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_Regist.php 21950 2012-07-02 11:42:51Z pineray $
 */
class LC_Page_Create_Conference extends LC_Page_Ex {

    // {{{ properties

    // }}}
    // {{{ functions

    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {

        parent::init();
        $this->adminAuthorityCheck(AUTHORITY_CONFERENCE_ADMIN);
        $this->tpl_mainpage = 'conference/create_conference.tpl';

        $this->linkCss[] = 'css/datepickerImport.css';
        $this->link_javascript[] = "js/sapa/jquery.ui.core.js";
        $this->link_javascript[] = "js/sapa/jquery.ui.datepicker.js";
        $this->link_javascript[] = "js/sapa/jquery.ui.widget.js";

        $this->linkCss[] = 'css/sapaImport.css';
        //引入本页面javascript
        $this->link_javascript[] = "js/sapa/jquery.dataTables.js";
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
        $this->action();
        $this->sendResponse();

    }

    /**
     * Page のAction.
     *
     * @return void
     */
    function action() {
        switch ($this->getMode()) {

        	case 'confirm':

        		$this->getConferenceInfoFromPost();
        		$this->isConfirm= true;
				$this->htmlErrMsg = "";

				if ($_FILES["attachment"]["size"] > CONFERENCE_UP_MAX_SIZE )
				{
					$this->htmlErrMsg  = '<div class="messages error">';
					$this->htmlErrMsg .= '	<ul>';
					$this->htmlErrMsg .= '		<li>upload file is too big '.$_FILES["attachment"]["size"].'</li>';
					$this->htmlErrMsg .= '		<li>please smaller than '.(CONFERENCE_UP_MAX_SIZE/1024).'M</li>';
					$this->htmlErrMsg .= ' 	</ul>';
					$this->htmlErrMsg .= '</div>';

					$this->tpl_mainpage = 'conference/create_conference.tpl';
				}else{
				  $fileName = $_FILES["attachment"]["name"] ;
				  $fileType = $_FILES["attachment"]["type"] ;
				  $fileTmpName = $_FILES["attachment"]["tmp_name"];
				  $this->conferenceInfo['attachment'] = $_FILES["attachment"]["name"];

				  $date = new DateTime();
				  $fileName = $date->format('YmdHis')."_".$fileName;
				  $this->conferenceInfo['savedFileName'] = $fileName;

				  move_uploaded_file($fileTmpName,CONFERENCE_UP_REALDIR.$fileName);

				  $this->tpl_mainpage = 'conference/conference_confirm.tpl';
				}

        		break;
            case 'create':

            	$this->tpl_mainpage = 'conference/conference_confirm.tpl';
        		$this->createConference();
        		SC_Response_Ex::sendRedirect(HTTP_URL."conference/index.php");
                break;
            default:
                break;
        }
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }

    function  getConferenceInfoFromPost(){
    	$this->conferenceInfo=array();


    	$this->conferenceInfo['from'] = $this->getValFromPostOrGet('from');
    	$this->conferenceInfo['to'] = $this->getValFromPostOrGet('to');
    	$this->conferenceInfo['venue'] = $this->getValFromPostOrGet('venue');
    	$this->conferenceInfo['summary'] = $this->getValFromPostOrGet('summary');
    	$this->conferenceInfo['organizer'] = $this->getValFromPostOrGet('organizer');
    	$this->conferenceInfo['tel'] = $this->getValFromPostOrGet('tel');
    	$this->conferenceInfo['fax'] = $this->getValFromPostOrGet('fax');

    	$this->conferenceInfo['conferenceName'] = $this->getValFromPostOrGet('conferenceName');
		$this->conferenceInfo['membersFee'] = $this->getValFromPostOrGet('membersFee');
		$this->conferenceInfo['freeMembersFee'] = $this->getValFromPostOrGet('freeMembersFee');
    	$this->conferenceInfo['freeStudentMembersFee'] = $this->getValFromPostOrGet('freeStudentMembersFee');
    	$this->conferenceInfo['headcountLimit'] = $this->getValFromPostOrGet('headcountLimit');

    	$this->conferenceInfo['serviceName'] = $this->getValFromPostOrGet('serviceName');
    	$this->conferenceInfo['membersPrice'] = $this->getValFromPostOrGet('membersPrice');
    	$this->conferenceInfo['freeMembersPrice'] = $this->getValFromPostOrGet('freeMembersPrice');
    	$this->conferenceInfo['freeStudentMembersPrice'] = $this->getValFromPostOrGet('freeStudentMembersPrice');
    	$this->conferenceInfo['limit'] = $this->getValFromPostOrGet('limit');


    }



    function createConference(){
    	//$this->getConferenceInfoFromPost();

    	//插入数据库
        $objQuery = SC_Query::getSingletonInstance();
        $objQuery->begin();
        $objQuery->insert('mst_conference', $this->getInsertArr());

        $insertFeeArr = $this->getInsertFeeArr($objQuery);

        if(count($insertFeeArr)>0){
        	foreach ($insertFeeArr as $value) {
        		$objQuery->insert('tab_conference_fee', $value);
        	}
        }
    	//插入数据库时 出错了
		if ($objQuery->isError()) {
			$objQuery->rollback();
			SC_Utils_Ex::sfDispSiteError(FREE_ERROR_MSG, '', true, 'system error please contact the administrator.');
			break;
		}else{
			$objQuery->commit();
		}
    }


	function getInsertArr(){
    	$insertArr = array();

    	$insertArr['publisher_id'] = $this->objSess->getSession('user_id');			//发布者ID
		$insertArr['publisher_lastname'] = $this->objSess->getSession('lastname');      //发布者（姓）
		$insertArr['publisher_firstname'] = $this->objSess->getSession('firstname');     //发布者（名）
		$insertArr['conference_title'] =  $this->getValFromPostOrGet('conferenceName');        //会议名称
		$insertArr['contents'] = $this->getValFromPostOrGet('summary');                //会议概要
		$insertArr['organizer'] = $this->getValFromPostOrGet('organizer');               //会议组织方名
		$insertArr['conference_place'] = $this->getValFromPostOrGet('venue');        //举办地
		$insertArr['conference_tel'] = $this->getValFromPostOrGet('tel');          //大会
		$insertArr['conference_fax'] = $this->getValFromPostOrGet('fax');          //大会FAX

		$insertArr['persons_limite'] = $this->getValFromPostOrGet('headcountLimit');          //人数上限
		$insertArr['members_fee'] = $this->getValFromPostOrGet('membersFee');          //付费会员价格
		$insertArr['free_members_fee'] = $this->getValFromPostOrGet('freeMembersFee'); //免费会员价格
		$insertArr['free_student_members_fee'] = $this->getValFromPostOrGet('freeStudentMembersFee');

		$insertArr['start_time'] = $this->getValFromPostOrGet('from');              //举办时间
		$insertArr['close_time'] = $this->getValFromPostOrGet('to');              //结束时间


		$insertArr['conference_file'] = $this->getValFromPostOrGet('savedFileName');   //会议附件
		$insertArr['status'] = '0';                  //大会状态 0： 正常 1： 临时取消

		$insertArr['del_flg'] = '0';
		$insertArr['create_time'] = 'now()';
		$insertArr['update_time'] = 'now()';

		return $insertArr;
    }

    function getInsertFeeArr($objQuery){
    	$rsArr = array();


    	$serviceNameArr = $this->getValFromPostOrGet('serviceName');
		$membersPriceArr = $this->getValFromPostOrGet('membersPrice');
		$freeMembersPriceArr = $this->getValFromPostOrGet('freeMembersPrice');
		$freeStudentMembersPriceArr = $this->getValFromPostOrGet('freeStudentMembersPrice');
		$limitArr = $this->getValFromPostOrGet('limit');


		if(count($serviceNameArr) > 0
				 && count($membersPriceArr) > 0
				 && count($freeMembersPriceArr) > 0
				 && count($freeStudentMembersPriceArr) > 0
				 && count($limitArr) > 0){

			$rsId = $objQuery->select('LAST_INSERT_ID() AS id','mst_conference');
			foreach ($serviceNameArr as $key=>$value) {

				$rs = array();
				//$rs['id'] = '';//项目ID		id
				$rs['conference_id'] = $rsId[0]['id'];	//会议ID
				$rs['fees_name'] = $value;	//项目名
				$rs['member_price'] = $membersPriceArr[$key];	//价格
				$rs['free_member_price'] = $freeMembersPriceArr[$key];	//价格
				$rs['free_student_price'] = $freeStudentMembersPriceArr[$key];	//价格
				$rs['unit'] = '';	//单位
				$rs['head_limit'] = $limitArr[$key];	//价格
				$rs['del_flg'] = '0';	//削除フラグ
				$rs['create_time'] = 'now()';	//作成日時
				$rs['update_time'] = 'now()';	//更新日時

				$rsArr[] = $rs;
			}
		}

    	return $rsArr;
    }
}
